<?php

use think\response\Json;

if(!function_exists('api_success')){
    /**
     * 操作成功
     * @param string $msg
     * @param string $data
     * @param int $code
     * @return Json
     */
    function api_success($data = [], $msg = 'success', $code = 200)
    {
        return api_result($msg, $data, $code);
    }
}

if(!function_exists('api_error')){
    /**
     * 操作失败
     * @param string $msg
     * @param string $data
     * @param int $code
     * @return Json
     */
    function api_error($msg = 'fail', $data = [], $code = 500)
    {
        return api_result($msg, $data, $code);
    }
}

if(!function_exists('api_result')){
    /**
     * 返回json结果
     * @param string $msg
     * @param string $data
     * @param int $code
     * @return Json
     */
    function api_result($msg = 'fail', $data =[], $code = 500)
    {
        $header = [];
        //处理跨域请求问题
        if (config('api.cross_domain.allow')) {
            $header = ['Access-Control-Allow-Origin' => '*'];
            if (request()->isOptions()) {
                $header = config('api.cross_domain.header');
                return json('',200,$header);
            }
        }

        return json([
            'code' => $code,
            'msg'  => $msg,
            'data' => $data,
        ], $code, $header);
    }
}

if(!function_exists('api_unauthorized')){
    /**
     * 未授权
     * @param string $msg
     * @param string $data
     * @param int $code
     * @return Json
     */
    function api_unauthorized($msg = 'unauthorized', $data = '', $code = 401)
    {
        return api_result($msg, $data, $code);
    }
}

if(!function_exists('api_client_error')){
    /**
     * 客户端错误
     * @param string $msg
     * @param string $data
     * @param int $code
     * @return Json
     */
    function api_client_error($msg = 'client error', $data = '', $code = 400)
    {
        return api_result($msg, $data, $code);
    }
}

if(!function_exists('api_server_error')){
    /**
     * 服务端错误
     * @param string $msg
     * @param string $data
     * @param int $code
     * @return Json
     */
    function api_server_error($msg = 'server error', $data = '', $code = 500)
    {
        return api_result($msg, $data, $code);
    }
}

if(!function_exists('api_error_404')){
    /**
     * 资源或接口不存在
     * @param string $msg
     * @param string $data
     * @param int $code
     * @return Json
     */
    function api_error_404($msg = '404 not found', $data = '', $code = 404)
    {
        return api_result($msg, $data, $code);
    }
}

if(!function_exists('sendRequest')) {
    /**
     * GET 请求
     * @param string $url
     * @return mixed|string
     */
    function sendRequest($url)
    {

        if (function_exists('curl_exec')) {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
            curl_setopt($ch, CURLOPT_TIMEOUT, 1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $data = curl_exec($ch);
            $curl_errno = curl_errno($ch);
            curl_close($ch);
            if ($curl_errno > 0) {
                return 0;
            }

            return $data;
        }

        $opts = array(
            'http' => array(
                'method' => 'GET',
                'timeout' => 2,
            )
        );
        $context = stream_context_create($opts);
        $data = @file_get_contents($url, false, $context);
        if ($data) {
            return $data;
        }

        return 0;
    }
}

if(!function_exists('postRequest')) {
    /**
     * POST 请求
     * @param string $url
     * @param string $post_data
     * @return mixed|string
     */
    function postRequest($url, $post_data = '')
    {
        if (!$post_data) {
            return false;
        }

        $data = http_build_query($post_data);
        if (function_exists('curl_exec')) {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
            curl_setopt($ch, CURLOPT_TIMEOUT, 1);

            //不可能执行到的代码
            if (!$post_data) {
                curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
            } else {
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            }
            $data = curl_exec($ch);

            if (curl_errno($ch)) {
                $err = sprintf("curl[%s] error[%s]", $url, curl_errno($ch) . ':' . curl_error($ch));
                trigger_error($err);
            }

            curl_close($ch);
        } else {
            if ($post_data) {
                $opts = array(
                    'http' => array(
                        'method' => 'POST',
                        'header' => "Content-type: application/x-www-form-urlencoded\r\n" . 'Content-Length: ' . strlen($data) . "\r\n",
                        'content' => $data,
                        'timeout' => 2
                    )
                );
                $context = stream_context_create($opts);
                $data = file_get_contents($url, false, $context);
            }
        }

        return $data;
    }
}
